<style lang="less">
    @import "./main.less";
</style>
<template>
    <div class="main" :class="{'main-hide-text': shrink}">
        <div class="sidebar-menu-con" :style="{width: shrink?'60px':'200px', overflow: shrink ? 'visible' : 'auto'}">
            <shrinkable-menu 
                :shrink="shrink"
                :menu-list="menuList">
                <div slot="top" class="logo-con">
                    <img v-show="!shrink"  src="../images/logo.jpg" key="max-logo" />
                    <img v-show="shrink" src="../images/logo-min.jpg" key="min-logo" />
                </div>
            </shrinkable-menu>
        </div>
        <div class="main-header-con" :style="{paddingLeft: shrink?'60px':'200px'}">
            <div class="main-header">
                <div class="navicon-con">
                    <Button :style="{transform: 'rotateZ(' + (this.shrink ? '-90' : '0') + 'deg)'}" type="text" @click="toggleClick">
                        <Icon type="navicon" size="32"></Icon>
                    </Button>
                </div>
                <div class="header-middle-con">
                    <div class="main-breadcrumb">
                        <breadcrumb-nav :currentPath="currentPath"></breadcrumb-nav>
                    </div>
                </div>
                <div class="header-avator-con">
                    <fullScreen v-model="isFullScreen" @on-change="fullscreenChange"></fullScreen>
                    <div class="user-dropdown-menu-con">
                        <Row type="flex" justify="end" align="middle" class="user-dropdown-innercon">
                            <Dropdown transfer trigger="click" @on-click="handleClickUserDropdown">
                                <a href="javascript:void(0)">
                                    <span class="main-user-name">{{ userInfo.username }}</span>
                                    <Icon type="arrow-down-b"></Icon>
                                </a>
                                <DropdownMenu slot="list">
                                    <DropdownItem name="loginout" divided>退出登录</DropdownItem>
                                </DropdownMenu>
                            </Dropdown>
                            <Avatar icon="person" style="background: #619fe7;margin-left:10px;"></Avatar>
                        </Row>
                    </div>
                </div>
            </div>
        </div>
        <div class="single-page-con" :style="{left: shrink?'60px':'200px'}">
            <div class="single-page">
                <router-view></router-view>
            </div>
        </div>
    </div>
</template>
<script>
import Cookies from 'js-cookie';
import shrinkableMenu from './main-components/shrinkable-menu/shrinkable-menu.vue';
import fullScreen from './main-components/fullscreen.vue';
import breadcrumbNav from './main-components/breadcrumb-nav.vue';
import util from '../libs/util';
import Admin from './role/model/Admin';

export default {
    components: {
        shrinkableMenu,
        fullScreen,
        breadcrumbNav
    },
    data () {
        return {
            shrink: false,
            isFullScreen: false
        };
    },
    computed: {
        userInfo() {
            return this.$store.state.user.info;
        },
        currentPath () {
            return this.$store.state.app.currentPath; // 当前面包屑数组
        },
        menuList () {
            return this.$store.state.app.menuList;
        }
    },
    methods: {
        init () {
            this.api.post("me/get_info", null, res => {
                this.$store.commit('setUserInfo', Admin.parse(res.data)); // 保存用户信息
                this.$store.commit('updateMenulist', this.userInfo.permission); // 根据权限更新左侧列表
            });
            util.setCurrentPath(this, this.$router.currentRoute.name);
        },
        toggleClick () {
            this.shrink = !this.shrink;
        },
        handleClickUserDropdown (name) {
            if (name === 'ownSpace') {
                // util.openNewPage(this, 'ownspace_index');
                // this.$router.push({
                //     name: 'ownspace_index'
                // });
            } else if (name === 'loginout') {
                // 退出登录
                this.api.post("admin/logout", null, res => {
                    this.$store.commit('logout', this);
                    this.$router.push({
                        name: 'login'
                    });
                });
            }
        },
        fullscreenChange (isFullScreen) {
            // console.log(isFullScreen);
        }
    },
    watch: {
        '$route' (to) {
            util.setCurrentPath(this, to.name);
        }
    },
    mounted () {
        this.init();
    }
};
</script>
